﻿using DevExpress.Mvvm.DataAnnotations;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VideoLib.Config;
using VideoLib.Utils;

namespace VideoLib.ViewModels
{
    [POCOViewModel]
    public class SettingViewModel
    {
        public SettingViewModel()
        {
            FilePathList = AppConfig.Instance.VideoPath;
            PlayerPath = AppConfig.Instance.PlayerPath;
            ShowImage = AppConfig.Instance.ShowImage;
        }

        public virtual ObservableCollection<string> FilePathList { get; set; }
        public virtual string SelectedPath { get; set; }
        public virtual string PlayerPath { get; set; }
        public virtual bool ShowImage { get; set; }

        public void AddPath()
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.ShowDialog();
            if (!string.IsNullOrEmpty(dialog.SelectedPath) && !FilePathList.Contains(dialog.SelectedPath))
            {
                FilePathList.Add(dialog.SelectedPath);
            }

            SaveConfig();
        }

        public void DelPath()
        {
            FilePathList.Remove(SelectedPath);
            SaveConfig();
        }

        public void SelectPlayer()
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "可执行文件|*.exe";
            dialog.ShowDialog();
            if (!string.IsNullOrEmpty(dialog.FileName))
            {
                PlayerPath = dialog.FileName;
                SaveConfig();
            }
        }

        public void OnShowImageChanged()
        {
            SaveConfig();
        }

        private void SaveConfig()
        {
            AppConfig.Instance.VideoPath = FilePathList;
            AppConfig.Instance.PlayerPath = PlayerPath;
            AppConfig.Instance.ShowImage = ShowImage;
            AppConfig.Save();
        }
    }
}
